/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;
import utils.XMLUtils;
import xmlasm.bo.FeedbackBO;
import xmlasm.dao.FeedbackDAO;
import xmlasm.dto.Feedback;
import xmlasm.dto.User;
import xmlasm.sync.FilePathHolder;

/**
 *
 * @author Khanh
 */
public class FeedbackAction extends ActionSupport {

    private int feedbackID;
    private String titleFeedback;
    private String contentFeedback;
    private boolean feedbackXMLchanged;

    public boolean isFeedbackXMLchanged() {
        return feedbackXMLchanged;
    }

    public void setFeedbackXMLchanged(boolean feedbackXMLchanged) {
        this.feedbackXMLchanged = feedbackXMLchanged;
    }
    private String outputFO;

    public String getOutputFO() {
        return outputFO;
    }

    public void setOutputFO(String outputFO) {
        this.outputFO = outputFO;
    }

    public int getFeedbackID() {
        return feedbackID;
    }

    public void setFeedbackID(int feedbackID) {
        this.feedbackID = feedbackID;
    }

    public FeedbackAction() {
    }

    public String insert() throws Exception {
        Map session = ActionContext.getContext().getSession();
        User user = (User) session.get("USER");
        int userId = user.getUserId();
        Date date = new Date();
        Feedback f = new Feedback(userId, titleFeedback, contentFeedback, date);
        boolean result = FeedbackDAO.insertFeedback(f);
        if (result) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("notification", "Cảm ơn góp ý của bạn. Chúng tôi sẽ xem xét và cải thiện.");
            return SUCCESS;
        }
        return ERROR;
    }

    public String showFeedback() {
        String xmlFilePath = FilePathHolder.getInstance().getFeedbackXMLPath();
        File f = new File(xmlFilePath);

        //Neu ko co file feedback thi moi marshall
        if (!f.exists()) {
            FeedbackBO.loadFeedbackAndMarshall();
        }

        String webPath = FilePathHolder.getInstance().getWebPath();
        String outputXMLPath = webPath + "WEB-INF/dataXML/feedback-user.xml";
        File outputXML = new File(outputXMLPath);
        Date date = new Date();

        //Neu chua co file join, hoac file feedback co thay doi, hoac da qua 10p ko cap nhat file join
        if (!outputXML.exists() || feedbackXMLchanged
                || ((date.getTime() - outputXML.lastModified()) > 10 * 60 * 1000)) {

            //Thuc hien join 2 file feedback va user
            Map<String, String> params = new HashMap<String, String>();
            params.put("userXML", FilePathHolder.getInstance().getUserXMLPath());
            String xslFilePath = webPath + "WEB-INF/xsl/joinFeedback-User.xsl";
            XMLUtils.doTransform(FilePathHolder.getInstance().getFeedbackXMLPath(),
                    xslFilePath, outputXMLPath, params);
            try {
                //Validate file xml vua join
                XMLUtils.validateXML(outputXMLPath,
                        webPath + "WEB-INF/schema/feedback-user.xsd");
            } catch (SAXException ex) {
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("ERROR", ex.getMessage());
                return ERROR;
            }
        } else {
            //Khong lam gi ca
        }
        return SUCCESS;
    }

    public String delete() {
        HttpServletRequest request = ServletActionContext.getRequest();
        boolean rs1 = FeedbackDAO.deleteFeedback(feedbackID);
        boolean rs2 = FeedbackBO.deleteFeedbackFromXML(feedbackID);
        if (rs1 && rs2) {
            feedbackXMLchanged = true;
            request.setAttribute("notify", "Đã xóa feedback với ID: " + feedbackID);
            return SUCCESS;
        }
        request.setAttribute("ERROR", "Không thể xóa được feedback");
        return ERROR;
    }

    //Sau nay chain qua thang getPDF
    public String getFeedbackPDF() {
        String webPath = FilePathHolder.getInstance().getWebPath();
        String inputXML = webPath + "WEB-INF/dataXML/feedback-user.xml";
        String inputXSL = webPath + "WEB-INF/xsl/feedback-user-fo.xsl";
        outputFO = webPath + "WEB-INF/pdf/fo/feedback.fo";
        XMLUtils.doTransform(inputXML, inputXSL, outputFO);
        return SUCCESS;
    }

    /**
     * @return the titleFeedback
     */
    public String getTitleFeedback() {
        return titleFeedback;
    }

    /**
     * @param titleFeedback the titleFeedback to set
     */
    public void setTitleFeedback(String titleFeedback) {
        this.titleFeedback = titleFeedback;
    }

    /**
     * @return the contentFeedback
     */
    public String getContentFeedback() {
        return contentFeedback;
    }

    /**
     * @param contentFeedback the contentFeedback to set
     */
    public void setContentFeedback(String contentFeedback) {
        this.contentFeedback = contentFeedback;
    }
}
